﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.iss.inventory.Entity;
using com.iss.inventory.BusinessLogic;
using com.iss.inventory.CustomModels;
using com.iss.inventory.Controllers;

namespace com.iss.inventory.Controllers
{
    public class ViewAdjVoucherController
    {
        public ViewAdjVoucherController() { }

        public String getDateByAdjustID(String txtAdjustID){
            InvManagementEntities IME = new InvManagementEntities();
            AdjVoucher av;
            try
            {
                var adjustID = Convert.ToInt32(txtAdjustID);
                av = IME.AdjVouchers.First<AdjVoucher>(x => x.AdjustID == adjustID);
            }
            catch (Exception ex) {
                return null;
            }
            return Convert.ToString(av.DateIssued);
        }

        public List<VoucherDetailsModel> getVoucherByAdjustID(String txtAdjustID)
        {
            InvManagementEntities IME = new InvManagementEntities();
            List<VoucherDetailsModel> voucherList = new List<VoucherDetailsModel>();
            var adjustID=Convert.ToInt32(txtAdjustID);
            var voucherQuery = from voucher in IME.AdjVoucherDetails
                               where voucher.AdjustID == adjustID
                               select voucher;
            foreach(var voucher in voucherQuery) {
                VoucherDetailsModel vdm = new VoucherDetailsModel();
                vdm.ItemID = (int)voucher.ItemID; vdm.Reason = voucher.Reason;
                if (voucher.QtyAdjust >= 0)
                {
                    vdm.QtyAdj = "+" + Convert.ToString(voucher.QtyAdjust);
                }
                else {
                    vdm.QtyAdj = Convert.ToString(voucher.QtyAdjust);
                }
                voucherList.Add(vdm);
            }
            return voucherList; 
        }
    }
}
